package com.ccx.coachsite;

import com.ccx.coachsite.bus.LoginBus;
import com.ccx.coachsite.bus.SharedKey;
import com.ccx.coachsite.bus.TianyiLoginBus;
import com.ccx.coachsite.http.UrlConfig;
import com.ccx.coachsite.ui.login.LoginHelper;
import com.ccx.coachsite.ui.login.QQWeibo;
import com.ccx.coachsite.ui.login.SinaWeibo;
import com.ccx.coachsite.util.SharedPreferencesUtil;
import com.ccx.coachsite.util.StringUtil;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;

public class MainSplash extends Activity {

	static final int GO_LOGIN = 1;
	LoginBus mLoginBus;

	private QQWeibo mQQWeibo;
	private SinaWeibo mSinaWeibo;

	private boolean doWeiboLogin(String LoginType) {
		boolean result = false;
		if (!StringUtil.isEmpty(LoginType)) {
			if (LoginType.equals("1")) {
				mSinaWeibo = new SinaWeibo(this);
				mSinaWeibo.doWeiboLogin();
				result = true;
			} else if (LoginType.equals("2")) {
				mQQWeibo = new QQWeibo(this);
				mQQWeibo.doWeiboLogin();
				result = true;
			}
		}

		return result;
	}

	private void doLogin() {
		String mobile = SharedPreferencesUtil.getValue(MainSplash.this,
				SharedKey.Shared_Mobile_Key, "");
		String password = SharedPreferencesUtil.getValue(MainSplash.this,
				SharedKey.Shared_Password_Key, "");
		String LoginType = SharedPreferencesUtil.getValue(MainSplash.this,
				SharedKey.Shared_Login_Type, "");
		if (doWeiboLogin(LoginType))
			return;

		if (LoginType.equals("4")) {
			TianyiLoginBus loginBus = new TianyiLoginBus(MainSplash.this);
			loginBus.doTianyiLogin(false);
			return;
		}
		if (StringUtil.isEmpty(mobile) || StringUtil.isEmpty(password)) {
			Intent intent = new Intent(MainSplash.this,
					CoachSiteMainActivity.class);// LoginActivity.class);
			startActivity(intent);
			finish();
		} else {
			if (mLoginBus == null) {
				mLoginBus = new LoginBus(this);
				mLoginBus.setxpert(true);
				mLoginBus.setShowProcess(false);
			}

			mLoginBus.login(mobile, password);
		}
	}

	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case GO_LOGIN:
				doLogin();
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// List<ContactInfo> lts =
		// ContactsUtil.getContactinfoList(MainSplash.this);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main_splash);
		if (UrlConfig.TestVersion)
			findViewById(R.id.splash_test_text).setVisibility(View.VISIBLE);
		LoginHelper.doLoginStatic(MainSplash.this, "", "");
		Thread thread = new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// e.printStackTrace();
				}
				handler.sendEmptyMessage(GO_LOGIN);
			}
		});
		thread.start();
	}

}
